/*
 * -------------------------------------------------------------------------------
 * @FilePath     : app_gsensor.c
 * @Description  : application
 * Copyright (c) 2023 by NanoIC, All Rights Reserved. 
 * @Date         : 2023-11-14 18:00:16
 * @LastEditTime : 2023-11-25 18:28:16
 * @Author       : LvH
 * -------------------------------------------------------------------------------
 */


/* Includes ---------------------------------------------------------------*/
#include "app_gsensor.h"
#include "app_parameter.h"
#include "atan.h"
/* externs function--------------------------------------------------------*/

/* externs variablesr------------------------------------------------------*/

/* Private variablesr------------------------------------------------------*/

uint16_t global_gsensor_timer_tick;

uint8_t global_gsensor_timer_flag;
uint16_t global_gsensor_timer_t_tick;

//gsensor禁用标志

volatile bool global_gsensor_enable;
volatile bool global_gsensor_send;
volatile bool gsensor_info_send_flag;

uint8_t global_gsensor_start =0;
#define FLASH_GSENSOR_CAL_ADDR                   0xE000         /* SN地址1 */

#define GSENSOR_CAL_OFFSET_MAX  				 819
#define GSENSOR_CAL_OFFSET_LSB                   300         /* G-sensor校准offset */

uint16_t cal_offset = GSENSOR_CAL_OFFSET_LSB;

int16_t X,Y,Z;
uint8_t gsensor_cal[6]={0xff};
//gsensor初始化完成标志
volatile bool global_gsenser_init_flag;


uint8_t gsensor_raw_value_flag=0;
uint8_t gsensor_level_cal_flag=0;
uint8_t gsensor_cal_value_flag=0;
uint8_t gsensor_cal_cfg_flag=0;
uint8_t gsensor_data_up_flag=0;
uint8_t gsensor_clear_cal_data_flag=0;
uint8_t gsensor_cal_offset_flag=0;

extern uint8_t one_wire_bus_app_sleep_or_wakeup_flag;


uint8_t gsensor_rawdata[6] = {0};
uint8_t gsensor_send_data[6] = {0};
static uint8_t count = 0;


//原始值
short original_x;
short original_y;
short original_z;


//水平标准值
short coefficient_x;
short coefficient_y;
short coefficient_z;

//校准系数
short horizont_x=0;
short horizont_y=0;
short horizont_z=0;

void app_gsensor_to_pad(void)
{
//    one_wire_bus_app_send_g_sensor(X,Y,Z);
    #if APP_TEST_SENSOR_ERROR
        printf("gsensor:  %d, %d, %d\r\n",X,Y,Z);
    #endif
}

/**
 * @brief gsensor读取数据
 * 
 * @return uint8_t 返回值
 */
uint8_t Mc3416_Readdata(void)
{
    uint8_t keyboard_enable = 0;
    int16_t Xvalue,Yvalue,Zvalue;
    keyboard_enable=app_iic_read_register(G_SENSOR_ADDR,0x0d,1,gsensor_rawdata,6);
    Xvalue = (short)(((unsigned short)gsensor_rawdata[1] << 8) + (unsigned short)gsensor_rawdata[0]);
    Yvalue = (short)(((unsigned short)gsensor_rawdata[3] << 8) + (unsigned short)gsensor_rawdata[2]);
    Zvalue = (short)(((unsigned short)gsensor_rawdata[5] << 8) + (unsigned short)gsensor_rawdata[4]);

	
//	#if APP_HALL_DEBUG_LOG
//		printf("gsensor_rawdata = ");
//		console_log_num_byte(gsensor_rawdata,6);
//	#endif

	
	Xvalue = Xvalue - horizont_x;
	Yvalue = Yvalue - horizont_y;
	Zvalue = Zvalue - horizont_z;
	
	if (Xvalue > 4096)
	{
	Xvalue = 4096;
	}
	if (Yvalue > 4096)
	{
	Yvalue = 4096;
	}
	if (Zvalue > 4096)
	{
	Zvalue = 4096;
	}
	if (Xvalue < (-4096))
	{
	Xvalue = (-4096);
	}
	if (Yvalue < (-4096))
	{
	Yvalue = (-4096);
	}
	if (Zvalue < (-4096))
	{
	Zvalue = (-4096);
	}
	
	Xvalue=-Xvalue;
	Xvalue=Xvalue/4;
	gsensor_send_data[3]=(Xvalue>>8)&0xff;
	gsensor_send_data[2]=Xvalue&0xff;
	
	Yvalue=-Yvalue;
	Yvalue=Yvalue/4;
	gsensor_send_data[1]=(Yvalue>>8)&0xff;
	gsensor_send_data[0]=Yvalue&0xff;
	

	Zvalue=-Zvalue;
	Zvalue=Zvalue/4;
	gsensor_send_data[5]=(Zvalue>>8)&0xff;
	gsensor_send_data[4]=Zvalue&0xff;
	
	if(X == 0x1fff)
	{
		X=Xvalue;
	}
	if(Y == 0x1fff)
	{
		Y=Yvalue;
	}
	if(Z==0x1fff)
	{
		Z=Zvalue;
	}
	

	
//	if(abs(X-Xvalue)>=45)
//	{
//		X=Xvalue;
//		keyboard_enable=1;
//	}
//	if(abs(Y-Yvalue)>=45)
//	{
//		Y=Yvalue;
//		keyboard_enable=1;
//	}
//	if(abs(Z-Zvalue)>=45)
//	{
//		Z=Zvalue;
//		keyboard_enable=1;
//	}
//	
	static uint8_t cont = 0;
	static uint8_t cont2 = 0;
	static uint8_t flag = 0;
	int16_t X_offset=0,Y_offset=0,Z_offset=0;
	if(pad_lcd_state||global_gsensor_send)	
	{		

		X_offset=(int16_t)X-Xvalue;
		
		Y_offset=(int16_t)Y-Yvalue;
		
		Z_offset=(int16_t)Z-Zvalue;
		
		if(X_offset>=30 ||X_offset<=(-30)|| Y_offset>=30 ||Y_offset<=(-30)|| Z_offset>=30 ||Z_offset<=(-30))
		{
			X=Xvalue;
			Y=Yvalue;
			Z=Zvalue;
			global_gsensor_enable=true;
			flag=1;
			cont = 0;
			cont2++;
			#if APP_HALL_DEBUG_LOG
				printf("gsensor:  %d, %d, %d\r\n",Xvalue,Yvalue,Zvalue);
				printf("gsensor_offset:  %d, %d, %d\r\n",X_offset,Y_offset,Z_offset);
			#endif
		}else{
			cont2=0;
		}
		if(cont2>=3)
		{
			app_sleep_timer_clear();
		}
		global_gsensor_send = false;
	}
	if(flag)
	{
		
		if(cont>=10)
		{
			cont = 0;
			flag=0;
		}else{
			cont++;
			global_gsensor_enable=true;
		}
	}
//        #if APP_HALL_DEBUG_LOG
//            printf("gsensor:  %d, %d, %d\r\n",Xvalue,Yvalue,Zvalue);
//			printf("gsensor_offset:  %d, %d, %d\r\n",X_offset,Y_offset,Z_offset);
//        #endif

    return keyboard_enable;
}
/* Private function--------------------------------------------------------*/
/**
 * @brief ：定时器调用的键盘扫描相关函数,0.1mS调一次
 * 
 * @param
 * @return
 */
void app_gsensor_timer_tick_0_1ms_callback(void)
{
    global_gsensor_timer_t_tick ++;
    if(global_gsensor_timer_t_tick >= (400))
    {
		global_gsensor_timer_t_tick=0;
        global_gsensor_timer_flag = 1;
    }
   
}
uint8_t gsensor_cal_process(void)
{
	uint8_t ret=0;
	horizont_x = (short)(original_x - coefficient_x);
	horizont_y = (short)(original_y - coefficient_y);
	horizont_z = (short)(original_z - coefficient_z);
	

	gsensor_cal[0] = (uint8_t)(horizont_x & 0xFF); 		   // x 的低字节
	gsensor_cal[1] = (uint8_t)((horizont_x >> 8) & 0xFF);  // x 的高字节
	gsensor_cal[2] = (uint8_t)(horizont_y & 0xFF); 		   // y 的低字节
	gsensor_cal[3] = (uint8_t)((horizont_y >> 8) & 0xFF);  // y 的高字节
	gsensor_cal[4] = (uint8_t)(horizont_z & 0xFF);  	   // z 的低字节
	gsensor_cal[5] = (uint8_t)((horizont_z >> 8) & 0xFF);  // z 的高字节
	
	 #if FACTORY_MODE_ON
	printf("horizont: %d %d %d\n",horizont_x,horizont_y,horizont_z);
	#endif
	if(abs(horizont_x)>cal_offset || abs(horizont_y)>cal_offset ||abs(horizont_z)>cal_offset)
	{
		ret=3;
	}else{
		ret = one_wire_bus_app_write_acc(gsensor_cal,6);
		
	}
	 #if FACTORY_MODE_ON
	printf("write acc ret:%d\n",ret);
	#endif
	return ret;
}
/**
 * @brief gsensor 进程
 * 
 */
void factory_gsensor_process(void)
{
uint8_t factory_buf[15];
 
	if(gsensor_raw_value_flag)		//读取ACC原始值
	{
		uint8_t ret= app_iic_read_register(G_SENSOR_ADDR,0x0d,1,gsensor_rawdata,6);
		original_x = (short)(((unsigned short)gsensor_rawdata[1] << 8) + (unsigned short)gsensor_rawdata[0]);
		original_y = (short)(((unsigned short)gsensor_rawdata[3] << 8) + (unsigned short)gsensor_rawdata[2]);
		original_z = (short)(((unsigned short)gsensor_rawdata[5] << 8) + (unsigned short)gsensor_rawdata[4]);
		 
		factory_buf[0] = 0x14;
		factory_buf[1] = 0x03;
		factory_buf[2] = 0x01;
		factory_buf[3] = 0x01;
		factory_buf[4] = ret;
		memcpy(&factory_buf[5],gsensor_rawdata,6);
		one_wire_bus_app_manufactory_ack(factory_buf,11);
		
		gsensor_raw_value_flag=0;
	}
	if(gsensor_level_cal_flag)		//ACC水平校准
	{
		uint8_t ret= gsensor_cal_process();
		factory_buf[0] = 0x14;
		factory_buf[1] = 0x03;
		factory_buf[2] = 0x01;
		factory_buf[3] = 0x02;
		factory_buf[4] = ret;
		one_wire_bus_app_manufactory_ack(factory_buf,5);
		gsensor_level_cal_flag=0;
	}
		
	
	if(gsensor_cal_value_flag)		//ACC校准后的值（对比修正系数eg：（0,0,-4096）后的值）
	{
		uint16_t x,y,z;
		uint16_t x_temp,y_temp,z_temp;
		uint8_t ret= app_iic_read_register(G_SENSOR_ADDR,0x0d,1,gsensor_rawdata,6);;
		factory_buf[0] = 0x14;
		factory_buf[1] = 0x03;
		factory_buf[2] = 0x01;
		factory_buf[3] = 0x03;
		factory_buf[4] = ret;				
		x = (short)(((unsigned short)gsensor_rawdata[1] << 8) + (unsigned short)gsensor_rawdata[0]);
		y = (short)(((unsigned short)gsensor_rawdata[3] << 8) + (unsigned short)gsensor_rawdata[2]);
		z = (short)(((unsigned short)gsensor_rawdata[5] << 8) + (unsigned short)gsensor_rawdata[4]);
		
		x_temp = x - horizont_x;
		y_temp = y - horizont_y;
		z_temp = z - horizont_z;
		
		//处理gsensor的值（MSB）
		gsensor_rawdata[0] = (uint8_t)(x_temp & 0xFF); 		   // x 的低字节
		gsensor_rawdata[1] = (uint8_t)((x_temp >> 8) & 0xFF);  // x 的高字节
		gsensor_rawdata[2] = (uint8_t)(y_temp & 0xFF); 		   // y 的低字节
		gsensor_rawdata[3] = (uint8_t)((y_temp >> 8) & 0xFF);  // y 的高字节
		gsensor_rawdata[4] = (uint8_t)(z_temp & 0xFF);  	   // z 的低字节
		gsensor_rawdata[5] = (uint8_t)((z_temp >> 8) & 0xFF);  // z 的高字节
		memcpy(&factory_buf[5],gsensor_rawdata,6);
		one_wire_bus_app_manufactory_ack(factory_buf,11);
		gsensor_cal_value_flag=0;
	}
	if(gsensor_cal_cfg_flag)		//读取ACC校准系数（也就是原来与eg:（0，0，4096）对比后的差值）
	{
		uint8_t ret = one_wire_bus_app_read_acc(0,gsensor_cal);
		factory_buf[0] = 0x14;
		factory_buf[1] = 0x03;
		factory_buf[2] = 0x01;
		factory_buf[3] = 0x04;
		factory_buf[4] = ret;
		
//		gsensor_rawdata[0] = (uint8_t)(horizont_x & 0xFF); 		   // x 的低字节
//		gsensor_rawdata[1] = (uint8_t)((horizont_x >> 8) & 0xFF);  // x 的高字节
//		gsensor_rawdata[2] = (uint8_t)(horizont_y & 0xFF); 		   // y 的低字节
//		gsensor_rawdata[3] = (uint8_t)((horizont_y >> 8) & 0xFF);  // y 的高字节
//		gsensor_rawdata[4] = (uint8_t)(horizont_z & 0xFF);  	   // z 的低字节
//		gsensor_rawdata[5] = (uint8_t)((horizont_z >> 8) & 0xFF);  // z 的高字节
		
		memcpy(&factory_buf[5],gsensor_cal,6);
		one_wire_bus_app_manufactory_ack(factory_buf,11);
		gsensor_cal_cfg_flag=0;
	}
	if(gsensor_data_up_flag)		//ACC开始数据上报(一直上报)
	{
		uint16_t x,y,z,x_temp,y_temp,z_temp;
		uint8_t ret= app_iic_read_register(G_SENSOR_ADDR,0x0d,1,gsensor_rawdata,6);
		factory_buf[0] = 0x14;
		factory_buf[1] = 0x03;
		factory_buf[2] = 0x01;
		factory_buf[3] = 0x07;
		factory_buf[4] = ret;
		
		x = (short)(((unsigned short)gsensor_rawdata[1] << 8) + (unsigned short)gsensor_rawdata[0]);
		y = (short)(((unsigned short)gsensor_rawdata[3] << 8) + (unsigned short)gsensor_rawdata[2]);
		z = (short)(((unsigned short)gsensor_rawdata[5] << 8) + (unsigned short)gsensor_rawdata[4]);
		
		x_temp = x - horizont_x;
		y_temp = y - horizont_y;
		z_temp = z - horizont_z;
		
		//处理gsensor的值
		gsensor_rawdata[0] = (uint8_t)(x_temp & 0xFF); 		   // x 的低字节
		gsensor_rawdata[1] = (uint8_t)((x_temp >> 8) & 0xFF);  // x 的高字节
		gsensor_rawdata[2] = (uint8_t)(y_temp & 0xFF); 		   // y 的低字节
		gsensor_rawdata[3] = (uint8_t)((y_temp >> 8) & 0xFF);  // y 的高字节
		gsensor_rawdata[4] = (uint8_t)(z_temp & 0xFF);  	   // z 的低字节
		gsensor_rawdata[5] = (uint8_t)((z_temp >> 8) & 0xFF);  // z 的高字节
		memcpy(&factory_buf[5],gsensor_rawdata,6);
		one_wire_bus_app_manufactory_ack(factory_buf,11);
		
//		gsensor_data_up_flag=0;
	}
	if(gsensor_clear_cal_data_flag)
	{
		app_iwt_clr();
		drv_flash_lock_config(UNLOCK);
		boot_flash_unlock_flash(UNLOCK);
		app_iwt_clr();
		uint8_t ret = app_spi_flash_sector_or_block_erase(FLASH_FCT_ADDR1,FLASH_SECTOR_SIZE);
		app_iwt_clr();
		drv_flash_lock_config(AREA_00000_2FFFF);
		boot_flash_lock_flash(AREA_00000_2FFFF);
		
		horizont_x =0;
		horizont_y =0;
		horizont_z =0;
		 
		factory_buf[0] = 0x14;
		factory_buf[1] = 0x03;
		factory_buf[2] = 0x01;
		factory_buf[3] = 0x0B;
		factory_buf[4] = ret;
		one_wire_bus_app_manufactory_ack(factory_buf,5);
		gsensor_clear_cal_data_flag = 0;
	}
	if(gsensor_cal_offset_flag)
	{
		uint8_t ret = 0;
		if(cal_offset > GSENSOR_CAL_OFFSET_MAX)
		{
			ret = 1;
			cal_offset = GSENSOR_CAL_OFFSET_LSB;
		}
		factory_buf[0] = 0x14;
		factory_buf[1] = 0x03;
		factory_buf[2] = 0x01;
		factory_buf[3] = 0x05;
		factory_buf[4] = ret;
		memcpy(&factory_buf[5],&cal_offset,2);
		one_wire_bus_app_manufactory_ack(factory_buf,7);
		gsensor_cal_offset_flag=0;
	}
}
/**
 * @brief gsensor 进程
 * 
 */
void app_gsensor_process(void)
{
	    //上电先发心跳包机制
    if(global_key_power_on_flag < 1||ota_first_flag)
        return;
		
    if(!global_gsensor_timer_flag)
        return;
    global_gsensor_timer_flag = 0x00;
    //若已经进入15s休眠标志，停止gsenser的扫描
    if(one_wire_bus_app_sleep_or_wakeup_flag)
	{
		count=0;
		return;

	}
       

	if(factory_enter_flag)
	{
		factory_gsensor_process();
	}
	else 
    if(global_gsenser_init_flag)
    {
		uint8_t gsensor_enable_status;
		static uint8_t cnt=0;
//		if(csi_pin_read(PA015) == 0x00)
		{ 
			Mc3416_Readdata();
		
			 
			if(accel_info_send_flag)
			{
				regular_read_gsensor_40ms_data();
			}
		}
        
    }

	
}

/**
 * @brief 定时模拟获取sensor数据
 */
void regular_read_gsensor_40ms_data(void)
{
//	Mc3416_Readdata();

	static uint8_t gsensor_buf[30] = {0};
	    // 将数据填入gsensor_buf数组中
	if(count<5)
	{
		memcpy(&gsensor_buf[count*6],gsensor_send_data,6);
		count++;
	}else{
		count=5;
		for(uint8_t i=0;i<(count-1);i++)
		{
			gsensor_buf[i*6]=gsensor_buf[(i+1)*6];
			gsensor_buf[i*6+1]=gsensor_buf[(i+1)*6+1];
			gsensor_buf[i*6+2]=gsensor_buf[(i+1)*6+2];
			gsensor_buf[i*6+3]=gsensor_buf[(i+1)*6+3];
			gsensor_buf[i*6+4]=gsensor_buf[(i+1)*6+4];
			gsensor_buf[i*6+5]=gsensor_buf[(i+1)*6+5];
		}
		memcpy(&gsensor_buf[(count-1)*6],gsensor_send_data,6);
		
		if(global_gsensor_enable)
		{
			one_wire_bus_app_send_g_sensor(gsensor_buf);
			memset(gsensor_buf, 0, sizeof(gsensor_buf));  // 清空缓冲区
			count = 0;  // 重置计数器
			global_gsensor_enable=false;
			syn_100us_timer_tick=0;
			gsensor_info_send_flag=true;
		}
	}

}


//---------------------------------------------------------------
//gsensor 初始化

uint8_t setPowermode(Mc3416_MODE mode)
{
    unsigned char _bRegData = 0x00;
    if(mode == 1)
    _bRegData =0x1; //wkemode
    else
    _bRegData =0x3; // standymode

    _bRegData=(_bRegData|IPP_PUSHPULL|IAH_ACTIVELOW);
    return app_iic_write_register(G_SENSOR_ADDR,0x07,1,&_bRegData,1);
}

uint8_t setRate(Mc3416_ODR odr)
{
    unsigned char _bRegData = 0x00;
    _bRegData = odr;//1024hz
    return app_iic_write_register(G_SENSOR_ADDR,0x08,1,&_bRegData,1);
}

uint8_t setRangelpf(Mc3416_RANGE range)
{
    unsigned char _bRegData = 0x00;
    _bRegData =0x09|range;
    return app_iic_write_register(G_SENSOR_ADDR,0x20,1,&_bRegData, 1);
}

uint8_t setEnableInterrupt(void)
{
//	unsigned char _bRegData = 0xDF;
	unsigned char _bRegData = 0x01;
    return app_iic_write_register(G_SENSOR_ADDR,0x06,1,&_bRegData, 1);
}

void setAnymotionParameters(uint16_t thr,uint8_t debounce)
{
    unsigned char _bRegData = 0x00; 
    //anymotion
    /*without lpf working ,threshold can be bigger , otherwise it should be small*/
    /*1365count = 1000mg */
    _bRegData =(thr&0xff);
    app_iic_write_register(G_SENSOR_ADDR,0x43,1,&_bRegData, 1);
    udelay(500);
    _bRegData =(thr&0xff00)>>8;
    app_iic_write_register(G_SENSOR_ADDR,0x44,1,&_bRegData, 1);
    udelay(500);
    /*according to the samplerate ,the lower rate ,the smaller value*/
    _bRegData =debounce;
    app_iic_write_register(G_SENSOR_ADDR,0x45,1,&_bRegData, 1);
}
void setShakeParameters(uint16_t thr,uint16_t p2pduration,uint8_t cnt)
{
	unsigned char _bRegData = 0x00; 
	//anymotion
	/*without lpf working ,threshold can be bigger , otherwise it should be small*/
	/*1365count = 1000mg */
	_bRegData =(thr&0xff);
	app_iic_write_register(G_SENSOR_ADDR,0x46,1, &_bRegData, 1);
	_bRegData =(thr&0xff00)>>8;
	app_iic_write_register(G_SENSOR_ADDR,0x47,1, &_bRegData, 1);
	/*according to the samplerate ,the lower rate ,the smaller value*/
	_bRegData =p2pduration&0xff;
	app_iic_write_register(G_SENSOR_ADDR,0x48,1, &_bRegData, 1);
	if(cnt>7)
		cnt=7;
	_bRegData =((p2pduration&0xff00)>>8|(cnt<<4));
	app_iic_write_register(G_SENSOR_ADDR,0x49,1, &_bRegData, 1);
}
void setMotionctrl( uint8_t anymotion,uint8_t tiltflip ,uint8_t shake,uint8_t tilt35)
{
    unsigned char _bRegData = 0x00;
    _bRegData = (tiltflip|(anymotion<<2)|(shake<<3)|(tilt35<<4));
    app_iic_write_register(G_SENSOR_ADDR,0x9,1,&_bRegData, 1);
}

void setIntsource( uint8_t anymotion,uint8_t tilt ,uint8_t shake,uint8_t flip,uint8_t tilt35,uint8_t acq,uint8_t autoclr)
{
    unsigned char _bRegData = 0x00;
    _bRegData = (tilt|(flip<<1)|(anymotion<<2)|(shake<<3)|(tilt35<<4)|(autoclr<<6)|(acq<<7));
    app_iic_write_register(G_SENSOR_ADDR,0x6,1,&_bRegData, 1);
}

uint8_t gsenser_init(void)
{
    unsigned char RegData = 0x00; 
    app_iic_read_register(G_SENSOR_ADDR,0x18,1,&RegData,1);
#if APP_HALL_DEBUG_LOG
    printf("0x18=%x\r\n",RegData);
#endif
    if((RegData&0xf0) != 0xa0)
        return 1;
    udelay(500);
    setPowermode(STANDBYMODE);
    udelay(500);
    setRate(MC3416_1024HZ);
    udelay(500);
	setRangelpf(MC3416_8G);
	return 0;
}

/**
 * @brief Gsensor初始化
 */
void app_Gsensor_init(void)
{
	if(global_gsenser_init_flag)
		return;

    if(!gsenser_init())
    {
//        udelay(500);
//
//
//		setAnymotionParameters(0x2aa,0x20);
//		udelay(500);
//
////		setShakeParameters(0xaaa,0x555,0x1);
////		udelay(500);
//
//		setMotionctrl(1,0,0,0);
//		udelay(500);
//		setIntsource(1,0,0,0,0,0,1); // shake  & autoclr
		udelay(500);
        setPowermode(WAKEMODE);
////		udelay(10000);
        global_gsenser_init_flag = true;
		global_gsensor_timer_t_tick=0;
		global_gsensor_send=false;
//		global_gsensor_enable=false;
		gsensor_info_send_flag=false;
		X=0x1fff;
		Y=0x1fff;
		Z=0x1fff;
		
    }
}


//验证eeprom
//		udelay(500);
//		unsigned char _bRegData = 0x17;
//		app_iic_write_register(0x50<<1,0x0000,2,&_bRegData,1);
//		udelay(10000);
//		unsigned char RecData = 0x00;
//		app_iic_read_register(0x50<<1,0x0000,2,&RecData,1);
void app_Gsensor_sleep_io_set(void)
{
    csi_pin_set_mux(G_sensor_PIN,PA015_GPD);

}



/**
 * @brief 设置进入低功耗
 * 
 * @param on true 唤醒  false休眠
 */
void app_gsensor_sleep_io_set(bool on)
{
    if(on)
    {
//        csi_gpio_port_dir(GPIOA0, (0x01ul << PINMASK_GSENSOR_IIC_INT), GPIO_DIR_INPUT);       // GPIOA1.11 输入
//        csi_gpio_port_pull_mode(GPIOA0, (0x01ul << PINMASK_GSENSOR_IIC_INT), GPIO_PULLUP);    // 上拉
//        global_gsensor_timer_tick = 0;

        setPowermode(WAKEMODE);
		count=0;
		global_gsensor_timer_t_tick=0;
		global_gsensor_timer_flag = 0x00;
		global_gsensor_send=true;
		global_gsensor_enable=true;
		gsensor_info_send_flag=false;
		X=0x1fff;
		Y=0x1fff;
		Z=0x1fff;
    }else
    {
//        app_Gsensor_sleep_io_set();

		setPowermode(STANDBYMODE);
    }
	udelay(200);
}
